package com.ocean.web.controller.index;

import com.ocean.common.core.domain.AjaxResult;
import com.ocean.common.utils.MyClassLoader;
import com.ocean.common.utils.StringUtils;
import com.ocean.common.utils.file.FileUtils;
import com.ocean.common.utils.file.UploadFile;
import com.ocean.common.utils.reflect.ReflectUtils;
import com.ocean.nc.domain.ONcFile;
import com.ocean.nc.service.IONcFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @author msy
 * @date 2024/1/25
 */
@Controller
@RequestMapping("home/compare")
public class CompareController {

    @Autowired
    IONcFileService ncFileService;

    @PostMapping
    @ResponseBody
    public AjaxResult getNcfile(@RequestParam String productId, @RequestParam Date date){
        List<ONcFile> list =  ncFileService.getNcFile(productId,date);
        return AjaxResult.success(list);
    }

    /**
     * 附件预览读取
     */
    @RequestMapping(params = "viewFile")
    public void viewFile(HttpServletRequest request, HttpServletResponse response) throws IllegalAccessException, InstantiationException {
        String fileid =getString(request.getParameter("fileid"),"");
        String subclassname = getString(request.getParameter("subclassname"), "com.jeecg.base.pojo.TSAttachment");
        Class fileClass = MyClassLoader.getClassByScn(subclassname);// 附件的实际类
        Object fileobj = fileClass.newInstance();
//        Object fileobj = systemService.getEntity(fileClass, fileid);
//        ReflectHelper reflectHelper = new ReflectHelper(fileobj);
        UploadFile uploadFile = new UploadFile(request, response);
        String contentfield = getString(request.getParameter("contentfield"), uploadFile.getByteField());
        byte[] content = (byte[])ReflectUtils.invokeGetter(fileobj, contentfield);
//        byte[] content = (byte[]) ReflectUtils.getMethodValue(contentfield);
        String path = getString(ReflectUtils.invokeGetter(fileobj, "realpath"),"");
//        String path = getString(reflectHelper.getMethodValue("realpath"));
        String extend = getString(ReflectUtils.invokeGetter(fileobj, "extend"),"");
//        String extend = oConvertUtils.getString(reflectHelper.getMethodValue("extend"));
        String attachmenttitle = getString(ReflectUtils.invokeGetter(fileobj, "attachmenttitle"),"");
//        String attachmenttitle = oConvertUtils.getString(reflectHelper.getMethodValue("attachmenttitle"));
        uploadFile.setExtend(extend);
        uploadFile.setTitleField(attachmenttitle);
        uploadFile.setRealPath(path);
        uploadFile.setContent(content);
        //uploadFile.setView(true);
        FileUtils.viewOrDownloadFile(uploadFile);
    }
    private String getString(String s, String defval) {
        if (StringUtils.isEmpty(s)) {
            return (defval);
        }
        return (s.trim());
    }
}
